package controllers

import (
	"encoding/json"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"strings"
	"time"
	"weiboxs.com/new_h_glasses/services"

	"weiboxs.com/new_h_glasses/enums"
	"weiboxs.com/new_h_glasses/models"
	"weiboxs.com/new_h_glasses/utils"
)

type ProductsController struct {
	BaseController
}

func (c *ProductsController) Prepare() {
	c.BaseController.Prepare()

	c.Data["activeSidebarUrl"] = "/product/index"
}

func (c *ProductsController) Index() {

	c.checkAuthor("product/index")

	c.showErpTypeid()

	c.display("", "products/index_footerjs.html")
}

func (c *ProductsController) DataGrid() {
	var params models.ErpProductsQueryParam
	json.Unmarshal(c.Ctx.Input.RequestBody, &params)
	query := c.GetString("query")
	if len(query) > 0 {
		params.Filter = query
	}
	params.DepartmentId = c.curUser.DepartmentId

	data, total := models.ErpProductsPageList(&params)

	c.jsonDataTable(total, data)
}

func (c *ProductsController) DataGridForExtDep() {
	var params models.ErpProductsQueryParam
	json.Unmarshal(c.Ctx.Input.RequestBody, &params)
	query := c.GetString("query")
	if len(query) > 0 {
		params.Filter = query
	}
	params.Depids = c.curUser.ExtendDepartmentList
	data, total := models.ErpProductsPageList(&params)
	c.jsonDataTable(total, data)
}

//生成产品编号
func (c *ProductsController) createIdentifier() string {
	//新增产品自动生成编码
	date := time.Now().Format("060102")
	count, _ := models.GetGetErpProductsQuerySeter().Count()
	if count <= 0 {
		count = 1
	}
	identifier := fmt.Sprintf("%s%05d", date, count+1)
	return identifier
}

func (c *ProductsController) Edit() {
	c.checkAuthor("product/edit")

	m := &models.ErpProducts{}

	if c.Ctx.Input.IsPost() {
		c.Save()
	}

	id, _ := c.GetInt64(":id")
	typeid, _ := c.GetInt("typeid")
	m.Id = id

	var (
		err            error
		template       string
		templateFooter string
	)
	template = "products/edit.html"
	if m.Id > 0 {
		m, err = models.GetErpProductsById(id, &c.curUser)
		if err != nil {
			c.pageError("找不到指定数据")
		}
	} else {
		m.Brand = &models.ErpBrand{Id: 0}
		m.UpperLimit = int64(9999)
		m.LowerLimit = int64(5)
		m.Suppliers = &models.ErpSuppliers{Id: 0}
		m.Number = c.createIdentifier()
		m.BarCode = m.Number

	}

	//获取品牌
	brand := models.ErpBrandList(&models.ErpBrandQueryParam{DepartmentId: c.curUser.DepartmentId})
	c.Data["brand"] = brand

	m.TypeId = typeid
	c.Data["m"] = m
	//获取供应商
	suppliers := models.ErpSuppliersList(&models.ErpSuppliersQueryParam{DepartmentId: c.curUser.DepartmentId})
	c.Data["suppliers"] = suppliers

	switch m.TypeId {
	case 10:
		template = "products/prozl/10.html"
		templateFooter = "products/prozl/10_footerjs.html"
	case 20:
		template = "products/prozl/20.html"
		templateFooter = "products/prozl/20_footerjs.html"
	case 30:
		template = "products/prozl/30.html"
		templateFooter = "products/prozl/30_footerjs.html"
	case 40:
		template = "products/prozl/40.html"
		templateFooter = "products/prozl/40_footerjs.html"
	case 41:
		template = "products/prozl/41.html"
		templateFooter = "products/prozl/41_footerjs.html"
	case 50:
		template = "products/prozl/50.html"
		templateFooter = "products/prozl/50_footerjs.html"
	case 60:
		template = "products/prozl/60.html"
		templateFooter = "products/prozl/60_footerjs.html"
	case 70:
		template = "products/prozl/70.html"
		templateFooter = "products/prozl/70_footerjs.html"
	}

	c.display(template, templateFooter, "shared/layout_dialog.html")
}

func (c *ProductsController) Save() {

	data := models.ErpProducts{}

	if err := c.ParseForm(&data); err != nil {
		c.jsonResult(enums.JRCodeFailed, "数据错误:"+err.Error(), "")
	}
	brandId, _ := c.GetInt64("BrandId")
	Suppliers, _ := c.GetInt64("Suppliers")
	data.Brand = &models.ErpBrand{Id: brandId}
	data.Suppliers = &models.ErpSuppliers{Id: Suppliers}
	data.DepartmentId = c.curUser.DepartmentId

	if data.Id > 0 { //更新数据
		if mr, err := models.GetErpProductsById(data.Id, &c.curUser); err != nil {
			c.jsonResult(enums.JRCodeFailed, "获取失败:"+err.Error(), "")
		} else {
			data.Status = mr.Status
		}

		data.UpdatedBy = c.curUser.RealName
		if len(data.Name) <= 0 {
			data.FullName = data.BrandName
		} else {
			data.FullName = data.Name
		}
		data.Name = data.FullName
		data.Pinyin = utils.ConvertPinyin(data.FullName)
		f, _ := utils.ReflectModelFields(&data)
		f = append(f, "ModelNumber", "ColourNumber")
		if err := data.ErpProductsEdit(f...); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "产品修改成功", "")
		}

	} else { //新增数据
		data.CreatedBy = c.curUser.RealName
		data.UpdatedBy = c.curUser.RealName

		data.Status = 1
		data.UpdatedAt = time.Now().Unix()
		data.CreatedAt = time.Now().Unix()

		var err error

		insertData := make([]*models.ErpProducts, 0)

		if len(data.Name) <= 0 {
			data.FullName = data.BrandName
			data.Name = data.FullName
		} else {
			data.FullName = data.Name
			data.Name = data.FullName
		}
		data.Pinyin = utils.ConvertPinyin(data.FullName)

		insertData = append(insertData, &data)

		if len(insertData) <= 0 {
			c.jsonResult(enums.JRCodeFailed, "没有产品可以录入", "")
		}

		if _, err = models.ErpProductsAdd(insertData); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		} else {
			c.jsonResult(enums.JRCodeSucc, "产品添加成功", "")
		}
	}

}

func (c *ProductsController) Delete() {

	c.checkAuthor("product/delete")

	id, err := c.GetInt64("id")
	if err != nil {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}

	if err := models.ErpProductsDel(id, c.curUser.DepartmentId); err != nil {
		c.jsonResult(enums.JRCodeFailed, "删除失败:"+err.Error(), "")
	} else {
		c.jsonResult(enums.JRCodeSucc, "删除成功", "")
	}
}

//选择库存产品
func (c *ProductsController) SelectProducts() {
	types := c.GetString("ty", "def")
	if types == "def" {
		c.display("", "products/selectproducts_footerjs.html", "shared/layout_dialog.html")
	} else if types == "t60" {
		c.display("products/pages/selectproducts_t60.html", "products/pages/selectproducts_t60_footerjs.html", "shared/layout_dialog.html")
	}
}

//获取配置信息
func (c *ProductsController) GetOptions() {
	typeid := services.GetAttributeForCacheSlice()
	var fields []string
	c.Ctx.Input.Bind(&fields, "fields")
	typevalue := make(map[string][]map[string]string)
	if len(fields) > 0 {
		for _, value := range fields {
			if _, ok := typeid[value]; ok {
				typevalue[value] = typeid[value]
			}
		}
	} else {
		typevalue = typeid
	}

	c.jsonResult(enums.JRCodeSucc, "ok", typevalue)
}

//snd
func (c *ProductsController) Snd() {
	c.checkAuthor("product/index")
	product_id, _ := c.GetInt64("product_id")
	if product_id == 0 {
		c.pageError("参数错误")
	}
	typeid, _ := c.GetInt("typeid")
	if c.Ctx.Input.IsPost() {
		var params models.ErpProductsSndQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Products = product_id
		params.Typeid = typeid
		data, total := models.ErpProductsSndPageList(&params)
		c.jsonDataTable(total, data)
	}

	c.Data["typeid"] = typeid
	c.Data["product_id"] = product_id

	c.display("", "products/snd_footerjs.html", "shared/layout_dialog.html")
}

//snd
func (c *ProductsController) SndList() {
	product_id, _ := c.GetInt64("product_id")
	if product_id == 0 {
		c.pageError("参数错误")
	}

	if c.Ctx.Input.IsPost() {
		var params models.ErpProductsSndQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		params.Products = product_id
		data, total := models.ErpProductsSndPageList(&params)
		c.jsonDataTable(total, data)
	}
	pro, _ := models.GetErpProductsById(product_id, &c.curUser)
	if pro.Id == 0 {
		c.pageError("商品不存在")
	}

	c.Data["typeid"] = pro.TypeId
	c.Data["product_id"] = product_id

	c.display("", "products/sndlist_footerjs.html", "shared/layout_dialog.html")
}

//新增参数
func (c *ProductsController) SndInsert() {
	c.checkAuthor("product/index")

	product_id, _ := c.GetInt64("product_id")
	if product_id == 0 {
		c.pageError("参数错误")
	}
	typeid, _ := c.GetInt("typeid")
	//判断是否属于本部门
	pro, _ := models.GetErpProductsById(product_id, &c.curUser)
	if pro.Id == 0 {
		c.pageError("商品不存在")
	}

	if c.Ctx.Input.IsPost() {
		c.SndSaveInsert(pro, typeid)
	}

	var template, templateFooter string

	switch typeid {
	case 10:
		template = "products/snd/10.html"
		templateFooter = "products/snd/10_footerjs.html"
	case 20:
		template = "products/snd/20.html"
		templateFooter = "products/snd/20_footerjs.html"
	case 30:
		template = "products/snd/30.html"
		templateFooter = "products/snd/30_footerjs.html"
	case 40:
		template = "products/snd/40.html"
		templateFooter = "products/snd/40_footerjs.html"
	case 41:
		template = "products/snd/41.html"
		templateFooter = "products/snd/41_footerjs.html"
	case 70:
		template = "products/snd/70.html"
		templateFooter = "products/snd/70_footerjs.html"
	}

	c.Data["typeid"] = typeid
	c.Data["product_id"] = product_id

	c.display(template, templateFooter, "shared/layout_dialog.html")
}

//保存添加
func (c *ProductsController) SndSaveInsert(product *models.ErpProducts, typeid int) {
	var insert_data []*models.ErpProductsSnd
	switch typeid {
	case 10, 20, 40: //光学镜片
		sph, _ := c.GetFloat("sph")
		sph1, _ := c.GetFloat("sph1")
		step_sph, _ := c.GetFloat("step_sph")
		cyl, _ := c.GetFloat("cyl")
		cyl1, _ := c.GetFloat("cyl1")
		step_cyl, _ := c.GetFloat("step_cyl")
		if (sph1 - sph) < 0 {
			c.jsonResult(enums.JRCodeFailed, "球镜所选区间范围无法计算", "")
		}
		if (cyl1 - cyl) < 0 {
			c.jsonResult(enums.JRCodeFailed, "柱镜所选区间范围无法计算", "")
		}

		if (((sph1-sph)/step_sph)*((cyl1-cyl)/step_cyl) > 500) {
			c.jsonResult(enums.JRCodeFailed, "区间选择范围过大，请缩小范围后再添加", "")
		}

		for s := sph; s <= sph1; s += step_sph {
			for c := cyl; c <= cyl1; c += step_cyl {
				snd := new(models.ErpProductsSnd)
				snd.Products = product
				snd.Sph = fmt.Sprintf("%.2f", s)
				if strings.Index(snd.Sph, "-") == -1 && snd.Sph != "0.00" {
					snd.Sph = "+" + snd.Sph
				}
				if typeid == 40 {
					snd.Add = fmt.Sprintf("%.2f", c)
					if strings.Index(snd.Add, "-") == -1 && snd.Add != "0.00" {
						snd.Add = "+" + snd.Add
					}
				} else {
					snd.Cyl = fmt.Sprintf("%.2f", c)
					if strings.Index(snd.Cyl, "-") == -1 && snd.Cyl != "0.00" {
						snd.Cyl = "+" + snd.Cyl
					}
				}
				insert_data = append(insert_data, snd)
			}
		}
	case 30, 41, 70:
		type TempProdctSnd struct {
			ModelNumber  string
			ColourNumber string
			Size         string //尺寸
			Genre        string //框形
			FilmColor    string //膜层颜色
			FunctionInfo string //功效
			UpperLimit   string
			Norms        string //规格
			PlaceOrigin  string //产地
			Dnumber      string //批准文号
		}
		psnd := make([]*TempProdctSnd, 0)
		dataInfo := c.GetString("dataInfo")
		if len(dataInfo) > 0 {
			if err := json.Unmarshal([]byte(dataInfo), &psnd); err != nil {
				c.jsonResult(enums.JRCodeFailed, "获取属性参数错误", "")
			}
		} else {
			c.jsonResult(enums.JRCodeFailed, "获取属性参数错误", "")
		}
		if len(psnd) <= 0 {
			c.jsonResult(enums.JRCodeFailed, "获取属性参数错误", "")
		}

		for _, value := range psnd {
			snd := new(models.ErpProductsSnd)
			snd.Products = product
			snd.Model = strings.ToUpper(value.ModelNumber)
			snd.Color = strings.ToUpper(value.ColourNumber)
			snd.Size = strings.ToUpper(value.Size)
			snd.Genre = strings.ToUpper(value.Genre)
			snd.FunctionInfo = strings.ToUpper(value.FunctionInfo)
			snd.FilmColor = strings.ToUpper(value.FilmColor)
			insert_data = append(insert_data, snd)
		}

	default:
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}

	if len(insert_data) <= 0 {
		c.jsonResult(enums.JRCodeFailed, "没有可添加的数据", "")
	}

	orm := orm.NewOrm()
	to, _ := orm.Begin()
	for k, _ := range insert_data {
		if err := models.ErpProductsSndSave(to, insert_data[k]); err != nil {
			to.Rollback()
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
	}
	to.Commit()
	c.jsonResult(enums.JRCodeSucc, "保存成功", "")
}

//修改参数
func (c *ProductsController) SndUpdate() {
	c.checkAuthor("product/index")

	id, _ := c.GetInt64("id")
	if id == 0 {
		c.pageError("参数错误")
	}
	product_id, _ := c.GetInt64("product_id")
	if product_id == 0 {
		c.pageError("参数错误")
	}
	typeid, _ := c.GetInt("typeid")
	snd := models.GetErpProductsSndById(id)
	if snd.Id == 0 {
		c.pageError("找不到信息")
	}
	//判断是否属于本部门
	pro, _ := models.GetErpProductsById(product_id, &c.curUser)
	if pro.Id == 0 {
		c.pageError("商品不存在")
	}
	if c.Ctx.Input.IsPost() {
		c.SndSaveUpdate(snd)
	}

	var template, templateFooter string
	switch typeid {
	case 10:
		template = "products/snd/edit_10.html"
		templateFooter = "products/snd/edit_10_footerjs.html"
	case 20:
		template = "products/snd/edit_20.html"
		templateFooter = "products/snd/edit_20_footerjs.html"
	case 30:
		template = "products/snd/edit_30.html"
		templateFooter = "products/snd/edit_30_footerjs.html"
	case 40:
		template = "products/snd/edit_40.html"
		templateFooter = "products/snd/edit_40_footerjs.html"
	case 41:
		template = "products/snd/edit_41.html"
		templateFooter = "products/snd/edit_41_footerjs.html"
	case 70:
		template = "products/snd/edit_70.html"
		templateFooter = "products/snd/edit_70_footerjs.html"
	}

	c.Data["typeid"] = typeid
	c.Data["product_id"] = product_id
	c.Data["snd"] = snd
	c.display(template, templateFooter, "shared/layout_dialog.html")
}

//保存修改
func (c *ProductsController) SndSaveUpdate(snd *models.ErpProductsSnd) {

	if err := c.ParseForm(snd); err != nil {
		c.jsonResult(enums.JRCodeFailed, "数据获取出错", "")
	}
	if err := models.ErpProductsSndUpdate(orm.NewOrm(), snd); err != nil {
		c.jsonResult(enums.JRCodeFailed, "更新失败"+err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "更新成功", "")
}

//更新状态
func (c *ProductsController) SndStatus() {
	c.checkAuthor("product/index")

	status, _ := c.GetInt("status")
	if status != 1 && status != 2 {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}

	var ids []int64
	c.Ctx.Input.Bind(&ids, "id")
	if len(ids) <= 0 {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}

	if err := models.ErpProductsSndStauts(ids, status); err != nil {
		c.jsonResult(enums.JRCodeFailed, "更新失败:"+err.Error(), "")
	} else {
		c.jsonResult(enums.JRCodeSucc, "更新成功", "")
	}
}

func (c *ProductsController) Price() {
	c.checkAuthor("product/index")

	product_id, _ := c.GetInt64(":id")
	if product_id == 0 {
		c.pageError("参数错误")
	}

	if c.Ctx.Input.IsPost() {
		var params models.ErpProductsPriceQueryParam
		json.Unmarshal(c.Ctx.Input.RequestBody, &params)
		if params.Department == 0 {
			params.Department = c.curUser.DepartmentId
		}
		params.Products = product_id

		data, total := models.ErpProductsPricePageList(&params)
		c.jsonDataTable(total, data)
	}

	pro, _ := models.GetErpProductsById(product_id, &c.curUser)
	if pro.Id == 0 {
		c.pageError("商品不存在")
	}
	c.Data["product"] = pro
	c.display("", "products/price_footerjs.html", "shared/layout_dialog.html")
}

//普通添加
func (c *ProductsController) PriceAdd() {
	c.checkAuthor("product/index")
	product_id, _ := c.GetInt64("product_id")
	if product_id == 0 {
		c.pageError("参数错误")
	}
	product, _ := models.GetErpProductsById(product_id, &c.curUser)
	if product.Id == 0 {
		c.pageError("商品不存在")
	}
	if c.Ctx.Input.IsPost() {

		TradePrice, _ := c.GetFloat("TradePrice")
		SuggestedPrice, _ := c.GetFloat("SuggestedPrice")
		CustomTradePrice, _ := c.GetFloat("CustomTradePrice")
		CustomSuggestedPrice, _ := c.GetFloat("CustomSuggestedPrice")
		product_id, _ := c.GetInt64("product_id")
		snd_id, _ := c.GetInt64("snd_id")
		sph_max, _ := c.GetFloat("sph_max")
		sph_min, _ := c.GetFloat("sph_min")
		cyl_max, _ := c.GetFloat("cyl_max")
		cyl_min, _ := c.GetFloat("cyl_min")

		var depids []int64
		c.Ctx.Input.Bind(&depids, "DepartmentId")
		if len(depids) == 0 {
			c.jsonResult(enums.JRCodeFailed, "请选择门店", "")
		}
		if product_id == 0 {
			c.jsonResult(enums.JRCodeFailed, "产品不存在", "")
		}

		for _, depid := range depids {
			price := new(models.ErpProductsPrice)
			price.Department = &models.Department{Id: depid}
			price.TradePrice = TradePrice
			price.SuggestedPrice = SuggestedPrice
			price.CustomTradePrice = CustomTradePrice
			price.CustomSuggestedPrice = CustomSuggestedPrice
			price.Products = product
			price.Snd = &models.ErpProductsSnd{Id: snd_id}
			price.SphMax = sph_max
			price.SphMin = sph_min
			price.CylMax = cyl_max
			price.CylMin = cyl_min
			if err := models.ErpProductsPriceAdd(price); err != nil {
				c.jsonResult(enums.JRCodeFailed, err.Error(), "")
			}
		}
		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}

	c.Data["product"] = product

	var template, templateFooter string
	switch product.TypeId {
	case 10:
		template = "products/price/add_10.html"
		templateFooter = "products/price/add_10_footerjs.html"
	case 20:
		template = "products/price/add_20.html"
		templateFooter = "products/price/add_20_footerjs.html"
	case 30:
		template = "products/price/add_30.html"
		templateFooter = "products/price/add_30_footerjs.html"
	case 40:
		template = "products/price/add_40.html"
		templateFooter = "products/price/add_40_footerjs.html"
	case 41:
		template = "products/price/add_41.html"
		templateFooter = "products/price/add_41_footerjs.html"
	case 50:
		template = "products/price/add_50.html"
		templateFooter = "products/price/add_50_footerjs.html"
	case 60:
		template = "products/price/add_60.html"
		templateFooter = "products/price/add_60_footerjs.html"
	case 70:
		template = "products/price/add_70.html"
		templateFooter = "products/price/add_70_footerjs.html"
	}
	c.display(template, templateFooter, "shared/layout_dialog.html")
}

//SND新增
func (c *ProductsController) PriceAddSnd() {
	c.checkAuthor("product/index")
	product_id, _ := c.GetInt64("product_id")
	if product_id == 0 {
		c.pageError("参数错误")
	}
	product, _ := models.GetErpProductsById(product_id, &c.curUser)
	if product.Id == 0 {
		c.pageError("商品不存在")
	}
	if c.Ctx.Input.IsPost() {

		TradePrice, _ := c.GetFloat("TradePrice")
		SuggestedPrice, _ := c.GetFloat("SuggestedPrice")
		CustomTradePrice, _ := c.GetFloat("CustomTradePrice")
		CustomSuggestedPrice, _ := c.GetFloat("CustomSuggestedPrice")
		product_id, _ := c.GetInt64("product_id")
		department_id, _ := c.GetInt64("DepartmentId")

		var snd_id []int64
		c.Ctx.Input.Bind(&snd_id, "snd_id")
		if len(snd_id) == 0 {
			c.jsonResult(enums.JRCodeFailed, "请选择需要设定的参数", "")
		}
		if product_id == 0 {
			c.jsonResult(enums.JRCodeFailed, "产品不存在", "")
		}
		if department_id == 0 {
			c.jsonResult(enums.JRCodeFailed, "门店不存在", "")
		}
		for _, snd := range snd_id {
			price := new(models.ErpProductsPrice)
			price.Department = &models.Department{Id: department_id}
			price.TradePrice = TradePrice
			price.SuggestedPrice = SuggestedPrice
			price.CustomTradePrice = CustomTradePrice
			price.CustomSuggestedPrice = CustomSuggestedPrice
			price.Products = product
			price.Snd = &models.ErpProductsSnd{Id: snd}
			if err := models.ErpProductsPriceAdd(price); err != nil {
				c.jsonResult(enums.JRCodeFailed, err.Error(), "")
			}
		}
		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}
}

func (c *ProductsController) PriceEdit() {
	c.checkAuthor("product/index")
	price_id, _ := c.GetInt64("price_id")
	if price_id == 0 {
		c.pageError("参数错误")
	}
	m := models.GetErpProductsPriceById(price_id)
	if !c.checkDepControl(m.Department.Id) {
		c.pageError("找不到价格信息")
	}
	if c.Ctx.Input.IsPost() {

		TradePrice, _ := c.GetFloat("TradePrice")
		SuggestedPrice, _ := c.GetFloat("SuggestedPrice")
		CustomTradePrice, _ := c.GetFloat("CustomTradePrice")
		CustomSuggestedPrice, _ := c.GetFloat("CustomSuggestedPrice")
		is_public, _ := c.GetInt("is_public", 0)
		price := new(models.ErpProductsPrice)
		price.Id = price_id
		price.Snd = m.Snd
		price.Department = m.Department
		price.Products = m.Products
		price.TradePrice = TradePrice
		price.SuggestedPrice = SuggestedPrice
		price.CustomTradePrice = CustomTradePrice
		price.CustomSuggestedPrice = CustomSuggestedPrice
		price.IsPublic = is_public
		if err := models.ErpProductsPriceSave(price); err != nil {
			c.jsonResult(enums.JRCodeFailed, err.Error(), "")
		}
		c.jsonResult(enums.JRCodeSucc, "ok", "")
	}

	c.Data["m"] = m

	c.display("", "products/priceedit_footerjs.html", "shared/layout_dialog.html")
}

//删除价格
func (c *ProductsController) PriceDelete() {
	c.checkAuthor("product/index")
	price_id, _ := c.GetInt64("price_id")
	if price_id == 0 {
		c.jsonResult(enums.JRCodeFailed, "参数错误", "")
	}
	price := models.GetErpProductsPriceById(price_id);
	if !c.checkDepControl(price.Department.Id) {
		c.jsonResult(enums.JRCodeFailed, "找不到价格信息", "")
	}
	if err := models.DeleteErpProductsPriceById(price.Id); err != nil {
		c.jsonResult(enums.JRCodeFailed, err.Error(), "")
	}
	c.jsonResult(enums.JRCodeSucc, "ok", "")
}

//获取PriceSND价格
func (c *ProductsController) PriceSnd() {
	var params models.ErpProductsSndQueryParam
	json.Unmarshal(c.Ctx.Input.RequestBody, &params)
	if params.Department == 0 {
		params.Department = c.curUser.DepartmentId
	}
	data, total := models.GetSndNosetPrice(&params)
	c.jsonDataTable(total, data)
}
